const { con, Result } = require('./common');

let Feedback = async (user_id, business_id, feedback_context) => {
	let res = new Result();
	let sql = `select * from feedback`;
	let [rows] = await con.execute(sql);
	if (user_id) {
		if (rows.length == 0) {
			let sql1 = `insert into feedback (feedback_id,userf_id,feedback_context) values(1,${user_id},'${feedback_context}')`;
			let [rows1] = await con.execute(sql1);
		} else {
			let sql1 = `insert into feedback (feedback_id,userf_id,feedback_context) values(${rows[rows.length - 1].feedback_id + 1},${user_id},'${feedback_context}')`;
			let [rows1] = await con.execute(sql1);
		}
	}
	if (business_id) {
		if (rows.length == 0) {
			let sql1 = `insert into feedback (feedback_id,businessf_id,feedback_context) values(1,${business_id},'${feedback_context}')`;
			let [rows1] = await con.execute(sql1);
		} else {
			let sql1 = `insert into feedback (feedback_id,businessf_id,feedback_context) values(${rows[rows.length - 1].feedback_id + 1},${business_id},'${feedback_context}')`;
			let [rows1] = await con.execute(sql1);
		}
	}
	return res;
}
let Feedback_look = async () => {
	let res = new Result();
	let sql = `select * from feedback`;
	let [rows] = await con.execute(sql);
	let arr1 = [];
	for (let i = 0; i < rows.length; i++) {
		if (rows[i].userf_id) {
			let sql1 = `select * from user where user_id = ${rows[i].userf_id}`;
			let [rows1] = await con.execute(sql1);
			arr1[i] = rows1[0];
			arr1[i].feedback = 1;
		}
		if (rows[i].businessf_id) {
			let sql1 = `select * from business where business_id = ${rows[i].businessf_id}`;
			let [rows1] = await con.execute(sql1);
			arr1[i] = rows1[0];
			arr1[i].feedback = 2;
		}
		arr1[i].feedback_context = rows[i].feedback_context;
		arr1[i].feedback_id = rows[i].feedback_id;
	}
	res.data = { arr1 };
	return res;
}
let Feedback_delete = async (feedback_id) => {
	let res = new Result();
	let sql = `delete from feedback where feedback_id = ${feedback_id}`;
	let [rows] = await con.execute(sql);
	return res;
}
module.exports = { Feedback, Feedback_look, Feedback_delete };

